package com.xsoft.demo10;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class BannerActivity extends AppCompatActivity {
    private MZBannerView banner;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        banner = (MZBannerView) findViewById(R.id.banner);
        banner.setPages(new DataBaseHelper(this).query(), new MZHolderCreator() {
            @Override
            public MZViewHolder createViewHolder() {
                return new MZViewHolder() {
                    private ImageView imageView;

                    @Override
                    public View createView(Context context) {
                        imageView = new ImageView(context);
                        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        imageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(v.getContext(), GoodsDetailActivity.class)
                                        .putExtra("data", (GoodsInfo) imageView.getTag()));
                            }
                        });
                        return imageView;
                    }

                    @Override
                    public void onBind(Context context, int position, Object data) {
                        try {
                            imageView.setImageBitmap(BitmapFactory.decodeStream(getAssets().open(((GoodsInfo) data).getPictureAlbum())));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        imageView.setTag(data);
                    }
                };
            }
        });
    }
}
